<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { deptApi } from '@/api/sys/deptApi'
import type { TreeItem } from '@/components/Tree'
import { BasicTree } from '@/components/Tree'

defineOptions({ name: 'DeptTree' })
const emit = defineEmits(['select'])
const treeRef = ref()
const treeData = ref<TreeItem[]>([])

async function fetch() {
  treeData.value = (await deptApi.tree({ enabled: true })).rows
}

function handleSelect(keys) {
  emit('select', keys[0])
}

onMounted(() => {
  fetch()
})
</script>

<template>
  <div class="m-4 mr-0 overflow-hidden" v-bind="$attrs">
    <BasicTree
      ref="treeRef"
      title="部门列表"
      toolbar
      search
      tree-wrapper-class-name="h-[calc(100%-35px)] overflow-auto"
      :click-row-to-expand="false"
      :tree-data="treeData"
      :field-names="{ key: 'deptId', title: 'deptName' }"
      @select="handleSelect"
    />
  </div>
</template>
